<?php
namespace Api\Event;

use Api\Constract\IPosable;
/**
 * 新闻资讯
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class NewsEvent extends BaseEvent implements IPosable
{
    /**
     * 模型对象
     */
    protected $mod;

    protected function _initialize() {
        parent::_initialize();
        $this->mod = D( 'Common/News', 'Service' );
    }

    public function lists($query = [], $p = 1, $limit = 10)
    {
        $map = [
            'status' => 1
        ];
        $map = array_merge($map, $query);
        $list = $this->mod->where($map)->page($p)->limit($limit)->order('create_time desc')->select();
        $res = [];

        $gEvent = event('game');
        foreach ($list as $value) {
            // id,cover,title,create_time,game
            uri_format($value['cover']);
            $news = [
                'id' => $value['id'],
                'cover' => $value['cover'],
                'title' => $value['title'],
                'detail' => $value['detail']?:'',
                'create_time' => $value['create_time'],
                'is_hot' => $value['is_hot'],
                'from' => $value['from'],
            ];
            if ($value['game_id']) {
                $game = $gEvent->info($value['game_id']);
                $news['game'] = $game['name'];
            }
            $res[] = $news;
        }
        return $res;
    }

    public function count($query = [])
    {
        $map = [
            'status' => 1
        ];
        $map = array_merge($map, $query);
        $res = $this->mod->where($map)->count();
        return intval($res);
    }

    /**
     * 详情
     * @author liubo <liubo@ilongyuan.com.cn>
     * @param  integer $id
     * @param  boolean $video_detail 是否获取视频详情
     */
    public function detail($id, $video_detail = true) {
        $news = $this->mod->where(['id' => $id, 'status' => 1])->find();
        if ($news) {
            uri_format($news['cover']);
            if ($news['game_id']) {
                $game = event('game')->info(intval($news['game_id']));
                $news['game'] = $game['name'];
            }
            if ($news['match_id']) {
                $match = event('match')->info(intval($news['match_id']));
                $news['match'] = $match;
            }
            $vEvent = event('video');
            foreach ($news['content'] as &$value) {
                switch ($value['type']) {
                    case 'video':
                        if ($video_detail) {
                            $video = $vEvent->detail(intval($value['data']['id']), 0, false);
                            $value['data'] = $video;
                        }
                        break;
                    case 'image':
                        uri_format($value['data']['url']);
                        break;
                }
            }
        }
        return $news;
    }

    public function info($id)
    {
        return $this->detail($id, false);
    }

    public function setReadCount($id)
    {
        $this->mod->where(['id' => intval($id)])->setInc('read', 1);
    }
}